<?php

namespace app\service;

use app\exception\ErrnoPlus;
use app\model\AliyunProjectModel;
use share\exception\ApiException;
use share\service\BaseModelService;
use share\service\cache\CommonCacheKeyService;
use share\service\RedisService;
use share\utils\Helper;

class AliyunProjectService extends BaseModelService
{

    public static function getModel()
    {
        return new AliyunProjectModel;
    }

    public static function getAllByCache(){        
        $key = CommonCacheKeyService::getCacheKey(CommonCacheKeyService::CACHE_KEY_ALIYUN_PROJECT);        
        $data = RedisService::instance(RedisService::CACHE_DB_BASE)->getJson($key);
        if(empty($data)){       
            $allData = self::getAllByFilter([]);
            if(!$allData){
                return null;
            }            
            $data = Helper::listArrayColumn($allData,['id','project_key','name']);
            RedisService::instance(RedisService::CACHE_DB_BASE)->setJson($key,$data,86400);
        }
        return $data;
    }

    public static function delCache(){
        $key = CommonCacheKeyService::getCacheKey(CommonCacheKeyService::CACHE_KEY_ALIYUN_PROJECT); 
        RedisService::instance(RedisService::CACHE_DB_BASE)->del($key);
    }

    public static function fastGetById($id)
    {
        $data = self::getAllByCache();
        return Helper::filterOne($data,['id' => $id]);
    }

    /**
     * 编辑条目 新增或者修改
     */
    public static function edit($info)
    {
        $id = $info['id'];
        unset($info['id']);
        if ($id == 0) {
            self::addAliyunProject($info);
        } else {
            self::updateAliyunProject($id, $info);
        }
        self::delCache();
        return $info;
    }

    /**
     * 新增条目
     */
    public static function addAliyunProject($info)
    {        
        return self::add($info);
    }

    /**
     * 修改条目
     */
    public static function updateAliyunProject($id, $info)
    {
        $origin  = self::getBase($id);
        if (empty($origin)) {
            throw new ApiException("origin data is not exists", ErrnoPlus::COMMON_INVALID_INPUT);
        }
        return self::update($id, $info);
    }

    /**
     * 删除条目
     */
    public static function delById($id)
    {
        $origin = self::getBase($id);
        if (empty($origin)) {
            return;
        }
        $res = self::del($id);
        self::delCache();
        return $res;
    }

    /**
     * 页面查询条件
     */
    public static function buildCondition($filter)
    {
        $res = [];
        if (isset($filter['id']) && !empty($filter['id'])) {
            $res['id'] = $filter['id'];
        }
        if (isset($filter['name']) && !empty($filter['name'])) {
            $res['name'] = ['like', '%' . $filter['name'] . '%'];
        }
        if (isset($filter['project_key']) && !empty($filter['project_key'])) {
            $res['project_key'] = ['like', '%' . $filter['project_key'] . '%'];
        }
        if (isset($filter['description']) && !empty($filter['description'])) {
            $res['description'] = ['like', '%' . $filter['description'] . '%'];
        }
        return $res;
    }
}
